function ListNode(val, next) {
  this.val = (val===undefined ? 0 : val)
  this.next = (next===undefined ? null : next)
}
function createList(arr) {
  if(arr.length == 0)
    return null;
  let head = new ListNode(arr[0], null), headNode = head;
  for(let i = 1; i < arr.length; i++) {
    let temp = new ListNode(arr[i], null);
    head.next = temp;
    head = temp;
  }
  return headNode;
}
function showList(head) {
  while(head) {
    console.log(head.val);
    head = head.next;
  }
}
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
  if(head == null || head.next == null)
    return false;
  let slowNode = head, fastNode = head;
  while(slowNode != null && fastNode != null) {
    slowNode = slowNode.next;
    fastNode = fastNode.next;
    if(fastNode != null) {
      fastNode = fastNode.next;
    }
    if(slowNode === fastNode) {
      return true;
    }
  }
  return false;
};
let head = [3,2,0,-4];
head = createList(head);
console.log(hasCycle(head));
head = [1,2];
head = createList(head);
console.log(hasCycle(head));
head = [1];
head = createList(head);
console.log(hasCycle(head));
